package com.sigmatrix.h5.web.controller.utils;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.auth.AuthenticationInfo;
import com.sigmatrix.h5.config.TableUtils;
import com.sigmatrix.h5.entity.AdminArea;
import com.sigmatrix.h5.entity.v2.CodeInspectionRecord;
import com.sigmatrix.h5.message.bigdata.BigDataInspectionMessage;
import com.sigmatrix.h5.service.AdminAreaService;
import com.sigmatrix.h5.service.common.ServiceHelper;
import com.sigmatrix.h5.service.db.CodeInspectionRecordDBService;
import com.sigmatrix.h5.service.inspect.CodeInspectionRecordResp;
import com.sigmatrix.h5.service.inspect.ICodeInspect;
import com.sigmatrix.h5.service.inspect.InspectResult;
import com.sigmatrix.h5.service.log.BigDataLogger;
import com.sigmatrix.h5.web.args.QueryQrCodeParamV2;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="稽查测试")
@RestController
@RequestMapping("/util/test/inspect")
public class TestCodeInspectionRecordController {
	
	@Autowired
	private CodeInspectionRecordDBService codeInspectionRecordDBService;
	
	@Autowired
	private ServiceHelper serviceHelper;
	
	
	@Autowired
	private AdminAreaService adminAreaService;
	
	
	@ApiOperation(value="保存稽查记录",response=CodeInspectionRecord.class)
	@PostMapping("/insert/esecode/{esecode}")
	public CodeInspectionRecord insert(@PathVariable String esecode,@RequestBody CodeInspectionRecord record) {
		record.setTablePrefix(TableUtils.tablePrefixBy(esecode));
		codeInspectionRecordDBService.insert(record);
		log2BigData(record, 1);
		return record;
	}
	
	
	/**
	 * 大数据日志记录
	 *
	 * @param record
	 */
	private void log2BigData(CodeInspectionRecord record, Integer topEseId) {
		try {
			BigDataInspectionMessage message = new BigDataInspectionMessage();
			BeanUtils.copyProperties(record, message);
			message.setTopEseId(topEseId);
			BigDataLogger.loggerInfo(message.getLogType(), message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	@ApiOperation("显示当前用户稽查记录")
	@GetMapping("/record/show/isok/{isok}/esecode/{esecode}/acctId/{acctId}")
	public List<CodeInspectionRecordResp> inspectRecordShow(@PathVariable String esecode,@PathVariable Integer acctId,@PathVariable boolean isok){
		List<CodeInspectionRecord> result = null;
		if(isok){			
			result = codeInspectionRecordDBService.findOKByUserId(esecode,acctId);
		}else{
			result = codeInspectionRecordDBService.findNGByUserId(esecode,acctId);
		}
		
		List<CodeInspectionRecordResp> resultResp = new ArrayList<>();
		for (CodeInspectionRecord codeInspectionRecord : result) {
			CodeInspectionRecordResp resp = new CodeInspectionRecordResp();
			resultResp.add(resp);
			BeanUtils.copyProperties(codeInspectionRecord, resp);//copy
			AdminArea pro = adminAreaService.findByIdFromCache(resp.getProvinceId());
			if(pro!=null){			
				resp.setProvinceName(pro.getName());
			}
			AdminArea ci = adminAreaService.findByIdFromCache(resp.getCityId());
			if(ci!=null){			
				resp.setCityName(ci.getName());
			}
			AdminArea co = adminAreaService.findByIdFromCache(resp.getCountyId());
			if(co!=null){			
				resp.setCountyName(co.getName());
			}
		}
		return resultResp;
	}
	
	@ApiOperation("稽查执行。")
	@GetMapping("/process")
	public InspectResult process(){
		AuthenticationInfo user = new AuthenticationInfo();
		
		user.setAcctId(8888);
		user.setLoginName("ww@88.com");
		user.setEseCode("sigmatrix");
		QueryQrCodeParamV2 vo = new QueryQrCodeParamV2();
		vo.setCodeType(3);
		vo.setQrCode("YUKGMCQFR*RXXODLW23");
		vo.setLat("31.241985");
		vo.setLng("121.462");
		InspectResult inspectResult = serviceHelper.selectService("sigmatrix",ICodeInspect.class,true).inspect(vo, user.getEseCode(),user);
		return inspectResult;
	}
	
	@ApiOperation("稽查执行。")
	@PostMapping("/process/esecode/{esecode}/acctId/{acctId}")
	public InspectResult process(@PathVariable String esecode,@PathVariable Integer acctId,@RequestBody QueryQrCodeParamV2 vo){
		AuthenticationInfo user = new AuthenticationInfo();
		
		user.setAcctId(acctId);
		user.setLoginName("ww@88.com");
		user.setEseCode(esecode);
		
//		QueryQrCodeParamV2 vo = new QueryQrCodeParamV2();
//		vo.setCodeType(3);
//		vo.setQrCode("YUKGMCQFR*RXXODLW23");
//		vo.setLat("31.241985");
//		vo.setLng("121.462");
		
		InspectResult inspectResult = serviceHelper.selectService(esecode,ICodeInspect.class,true).inspect(vo, user.getEseCode(),user);
		return inspectResult;
	}
	
}

